package ru.susu.algebra.centralunits.alternating.tex.local;

import java.util.Map.Entry;

import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.centralunits.alternating.initializers.PartitionsInitializer;
import ru.susu.algebra.centralunits.alternating.initializers.QuadraticFieldsInitializer;
import ru.susu.algebra.centralunits.alternating.initializers.SpecialRowsInitializer;
import ru.susu.algebra.field.quadratic.QuadraticField;
import ru.susu.algebra.jtex.ITexElement;
import ru.susu.algebra.jtex.StringTexElement;
import ru.susu.algebra.jtex.TexBeginEndElement;
import ru.susu.algebra.jtex.formula.FormulaTexElement;
import ru.susu.algebra.jtex.formula.GreekSymbols;
import ru.susu.algebra.jtex.formula.MathSymbols;
import ru.susu.algebra.jtex.utils.TexUtils;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;
import ru.susu.algebra.util.StringUtilities;

/**
 * @author akargapolov
 * @since: 02.09.2010
 */
public class GammaLemmaGenerator extends MathMethodWithInitializers<ITexElement>
{
	private static final Class[] DEPENDENCIES = {SpecialRowsInitializer.class, PartitionsInitializer.class, QuadraticFieldsInitializer.class};

	@Override
	protected ITexElement directRun(IPropertySource ps) throws Exception
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ITexElement run(IPropertySource ps) throws Exception
	{
		super.run(ps);

		TexBeginEndElement lemma = TexBeginEndElement.lemma();
		String lambdaMinus1 = FormulaTexElement.brackets(GreekSymbols.LAMBDA + " - 1");
		Integer rows = PartitionsInitializer.getNumberRows(ps);
		lemma.addSubElement(StringTexElement.text("Пусть $u(" + GreekSymbols.LAMBDA + ") = " +
				FormulaTexElement.sum("i=1", rows) + GreekSymbols.GAMMA + TexUtils.index("i") +
				"y" + TexUtils.index("i") + "$. Тогда"));

		lemma.addSubElement(TexBeginEndElement.equation().addSubElement(StringTexElement.text(
				GreekSymbols.GAMMA + TexUtils.index(1) + " = 1 + " +
				FormulaTexElement.frac(MathSymbols.DEG + GreekSymbols.CHI + MathSymbols.TR + lambdaMinus1, "z") + ",\n" +
				GreekSymbols.GAMMA + TexUtils.index("i") + " = " + FormulaTexElement.frac(
						MathSymbols.TR + FormulaTexElement.brackets(GreekSymbols.CHI + FormulaTexElement.brackets("x" + TexUtils.index("i")) + lambdaMinus1), "z") +
				FormulaTexElement.text("для") + "i" + FormulaTexElement.in("2," + MathSymbols.DOTS + "," + rows) + ".")));

		StringBuffer buffer = new StringBuffer("Здесь ");
		int count = 0;
		for (Entry<Partition, QuadraticField> entry : QuadraticFieldsInitializer.getRow2Field(ps).entrySet())
		{
			Partition partition = entry.getKey();
			if (!SpecialRowsInitializer.isSpecialNotIntegerRow(partition, ps))
			{
				continue;
			}
			QuadraticField field = entry.getValue();

			buffer.append(count > 0 ? ", " : StringUtilities.EMPTY_STRING);
			buffer.append(StringUtilities.NEW_LINE);
			buffer.append(FormulaTexElement.formula(MathSymbols.TR + TexUtils.index(field.getGenerateInteger().getBDInSqrt())).generateContent());
			buffer.append(MathSymbols.LONG_DEFICE).append(" след в поле ");
			buffer.append(FormulaTexElement.formula("\\Q(" + FormulaTexElement.sqrt(field.getGenerateInteger().getD()) + ")").generateContent());

			count++;
		}
		buffer.append(".");

		lemma.addSubElement(StringTexElement.text(buffer.toString()));

		return lemma;
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return DEPENDENCIES;
	}

}
